"""
@file Senior.py
@author GalaktiK (jacobliu001@qq.com)
@version 1.0
@date 2021-04-25

@copyright Copyright (c) 2021, Published under License GPLv2
"""

# Simple input processing
SS = input()
# gets first number n
n = int(SS.split()[0])
# a contingency list. As the vertices are labelled 1 through 9, we store 10 lists.
# (I don't particularly fancy contingency matrices)

# Note that we have node 0 as a virtual node,
# unidirectionally connected to all other nodes,
# to help with starting the recursive method.
conlist = [list() for _ in range(10)]
# more
for x in SS.split()[1:]:
    a = int(x[0])
    b = int(x[1])
    conlist[a].append(b) # normal contingency list operation
    conlist[0].append(a) # append a to virtual root node (0)
    conlist[0].append(b) # append b to virtual root node (0)
conlist[0] = sorted(set(conlist[0])) # remove all duplicate members, and sort the array.

def dfs(len, path) -> int:
    """A Depth-First-Search algorithm to find the desired answer.
    It returns the sum of all paths that begins with a certain prefix (as in the `path' parameter)
    Call with (len=0, path=0) to get the total solution.
    USES conlist[]

    Args:
        len (int): the current number of real nodes traversed (=the length of the path, including node 0)
        path (int): an integer describing the path prefix, where each digit represents the corresponding node.

    Returns:
        int: the sum of all paths that begins with a certain prefix
    """
    if len == n+1:
        return path
    cur:int = path % 10
    s:int = 0
    for x in conlist[cur]:
        mpath = str(path)
        if mpath.find(str(x)) == -1:
            s += dfs(len+1, path*10+x)
    return s

# The answer equals running the algorithm on the virtual node with initial length 0.
print(dfs(0,0))
